package cn.erp.app.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;


import cn.erp.app.vo.menu.MenuVo;
import cn.erp.common.model.SystemMenu;

public class MenuService {
	
	public static MenuService me = new MenuService();
	
	public Page<SystemMenu> getAllWithPaginate(int pageNumber,int pageSize) {
		return SystemMenu.dao.paginate(pageNumber, pageSize, "select * ", "from system_menu");
	}
	
	public List<SystemMenu> getAll() {
		return SystemMenu.dao.find("select * from system_menu");
	}
	
	public List<MenuVo> getMenus() {
		List<SystemMenu> rootMenus = getRootMenu();
		List<MenuVo> result = new ArrayList<MenuVo>();
		for(SystemMenu menu :rootMenus) {
			MenuVo menuvo = new MenuVo();
			menuvo.setId(menu.getId());
			menuvo.setMenuName(menu.getMenuName());
			menuvo.setIcon(menu.getIcon());
			menuvo.setUrl(menu.getUrl());
			List<SystemMenu> subMenus = getByPid(menu.getId());
			if(subMenus!=null&&subMenus.size()>0) {
				menuvo.setSubMenu(subMenus);
			}
			result.add(menuvo);
		}
		return result;
		
	}
	public List<SystemMenu> getRootMenu() {
		return SystemMenu.dao.find("select * from system_menu where level=0");
	}
	
	public List<SystemMenu> getByPid(long id) {
		return SystemMenu.dao.find("select * from system_menu where parentId=?",id);
	}
}
